package com.eclink.iot;


import com.eclink.utils.Log;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;


public class Api
{
  private static String TAG = "RFID";
  private static ServerSocket server_socket;
  private static Map<String, DeviceClient> devices = new HashMap<>(); private static Runnable process;
  
  public static boolean initialization(int port) {
    try {
      server_socket = new ServerSocket(port);
      Log.d(TAG, "Server started. Waiting for clients...");
      
      (new Thread(process)).start();
    }
    catch (Exception e) {
      e.printStackTrace();
      return false;
    } 
    
    return true;
  }
  static {
    process = (() -> {
        while (true) {
          try {
            while (true) {
              Socket clientSocket = server_socket.accept();
              Log.d(TAG, "Client connected: " + clientSocket);
              (new DeviceClient(clientSocket)).start();
              Thread.sleep(10L);
            } 

          } catch (Exception e) {
            e.printStackTrace();
          } 
        } 
      });
  }
  private static void online_notification(String uuid, DeviceClient client) {
    if (devices.containsKey(uuid)) {
      devices.remove(uuid);
    }
    devices.put(uuid, client);
  }
  
  private static void offline_notification(String uuid) {}
  
  private static class DeviceClient
    extends Thread
  {
    private Socket socket;
    
    public DeviceClient(Socket socket) {
      this.socket = socket;
    }
    
    public void run() {
      while (true) {
        try {
          while (true)
          { BufferedReader in = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            PrintWriter out = new PrintWriter(this.socket.getOutputStream(), true);
            
            String message = in.readLine();
            System.out.println("Received message from client: " + message);
            
            Thread.sleep(10L); }
        } catch (Exception e) {
          e.printStackTrace();
        } 
      } 
    }
  }
}


/* Location:              E:\code\work\eclink\eclink-iot-tcp-server\backend\libs\eclink-iot.jar!\com\eclink\iot\Api.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */